//企业搜索
import React, { Component } from 'react'
import {
    View,
    Text,
    TextInput,
    TouchableOpacity,
} from 'react-native';

import AsyncStorageUtil from '../../../util/AsyncStorageUtil';
import { Selectc_Style as styles } from '../../../../styles';
import Icon from 'react-native-vector-icons/Ionicons';
var Dimensions = require('Dimensions');
var width = Dimensions.get('window').width;

import { SelResumeByConditionsAction } from '../../../actions/resume_c';      //加载action
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

import Selectc_state from './selectc_state'
import Selectin_c from './selectin_c'
import { myAlert } from '../../../util/compatible';

class Select_c extends Component {
    constructor(props) {            //构造器只执行一次
        super(props);
        this.state = {
            workspace: '全国',        //地点
            resumePosition: null,       //简历名
            fieldName: null,           //领域
            educationLevel: null,      //学历
            resumeMm: null,             //薪资
            resumeIntentYm: null,       //工作年限
            horstate: false,         //显示历史记录或简历集合 
            dataSource: [],
        }
    }

    componentDidMount() {
        AsyncStorageUtil.get("selResumeHis").then((data) => {
            if (data != null) {
                this.setState({ dataSource: data });
            }
        });
    }

    componentWillUnmount() {
        AsyncStorageUtil.save("selResumeHis", this.state.dataSource).then(() => {
            this.props.navigation.goBack();
        });
    }

    render() {
        return (
            <View style={styles.selc_all}>

                <View style={styles.selc_top}>
                    <TouchableOpacity style={styles.selc_back} activeOpacity={1}
                        onPress={() => { this.props.navigation.goBack() }}>
                        <Icon
                            name={'ios-arrow-back'}
                            size={25}
                            color={'#ffffff'} />
                    </TouchableOpacity>

                    <View style={[styles.selc_inputout, { width: width * 0.75 }]}>

                        <TextInput
                            style={[styles.selc_input, { width: width * 0.75 }]}
                            placeholder="搜索简历"
                            underlineColorAndroid="transparent"
                            onChangeText={this._resumePosition.bind(this)}
                            defaultValue={this.state.resumePosition}
                            autoFocus={true}
                        />
                        <Icon
                            name={'ios-search'}
                            size={20}
                            color={'#aeaeae'} style={styles.selc_icon1} />
                    </View>

                    <TouchableOpacity
                        style={styles.selc_select}
                        activeOpacity={1}
                        onPress={() => {
                            if (this.state.resumePosition) {
                                this.props.actions.SelResumeByConditionsAction({
                                    page: 1,
                                    workspace: '全国',                                   //地点
                                    resumePosition: this.state.resumePosition,          //简历名
                                    fieldName: this.state.fieldName,                    //领域
                                    educationLevel: this.state.educationLevel,          //学历
                                    resumeMm: this.state.resumeMm,                      //薪资
                                    resumeIntentYm: this.state.resumeIntentYm,          //工作年限
                                });
                                this.dataSource(this.state.resumePosition)
                                this.setState({ horstate: true })
                            } else {
                                myAlert('请输入查询内容')
                            }
                        }}>
                        <Text style={styles.selc_toptext}>搜索</Text>
                    </TouchableOpacity>

                </View>
                {this.state.horstate ?
                    <Selectin_c

                        resumePosition={this.state.resumePosition}//简历名
                        fieldName={this.state.fieldName}           //领域
                        educationLevel={this.state.educationLevel}      //学历
                        resumeMm={this.state.resumeMm}             //薪资
                        resumeIntentYm={this.state.resumeIntentYm}       //工作年限
                        resume={this.props.resume.data}
                        isloading={this.props.resume.isloading}
                        ret={this.props.resume.ret}
                        actions={this.props.actions}
                        navigation={this.props.navigation} />

                    :

                    <Selectc_state
                        _resumeIntentYm={this._resumeIntentYm.bind(this)}
                        _resumeMm={this._resumeMm.bind(this)}
                        _educationLevel={this._educationLevel.bind(this)}
                        _fieldName={this._fieldName.bind(this)}
                        _horstate={this._horstate.bind(this)}
                        _resumePosition={this._resumePosition.bind(this)}
                        dataSource={this.state.dataSource}
                        _deldataSource={this._deldataSource.bind(this)}
                        actions={this.props.actions}
                        navigation={this.props.navigation} />
                }
            </View >

        )
    }

    //简历名称查询
    _resumePosition(a) {
        this.setState({ resumePosition: a });
    }

    //显示历史记录或职位集合
    _horstate() {
        this.setState({ horstate: true });
    }

    //删除历史记录
    _deldataSource(a) {
        let list = this.state.dataSource;
        for (var i = 0; i < list.length; i++) {
            if (list[i] == a) {
                list.splice(i, 1);
            }
        }
        this.setState({ dataSource: list });
    }

    //添加搜索记录
    dataSource(data) {
        if (data != '') {
            let list = [];
            list = this.state.dataSource;
            if (list.length == 0) {
                list.push(data);
                this.setState({ dataSource: list });
            } else if (0 < list.length && list.length < 5) {
                let a = false;
                for (let i = 0; i < list.length; i++) {
                    if (list[i] == data) {
                        a = true;
                    }
                }
                if (!a) {
                    list.push(data);
                    this.setState({ dataSource: list });
                }
            } else if (list.length == 5) {
                let b = false;
                for (let i = 0; i < list.length; i++) {
                    if (list[i] == data) {
                        b = true;
                    }
                }
                if (!b) {
                    list.splice(0, 1);
                    list.push(data);
                    this.setState({ dataSource: list });
                }
            }
        }
    }

    //领域
    _fieldName(name) {
        if (name != this.state.fieldName) {
            this.setState({ fieldName: name });
        }
    }

    //学历
    _educationLevel(name) {
        if (name != this.state.educationLevel) {
            this.setState({ educationLevel: name });
        }
    }

    //薪资
    _resumeMm(name) {
        if (name != this.state.resumeMm) {
            this.setState({ resumeMm: name });
        }
    }

    //工作年限
    _resumeIntentYm(name) {
        if (name != this.state.resumeIntentYm) {
            this.setState({ resumeIntentYm: name });
        }
    }

}

//组合action
var actions = {
    SelResumeByConditionsAction
}

//连接state到props
const mapStateToProps = state => {
    return {
        resume: state.SelResumeByConditionsReducer             //自己的reducers
    }
}

//连接action
const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators(actions, dispatch)
    }
}

//导出组合好的组件(组合后状态改变重新渲染组合组件的页面)
export default connect(
    mapStateToProps, mapDispatchToProps
)(Select_c);